package com.estate.admin.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.estate.admin.service.AdminService;
import com.estate.bean.Manager;
import com.estate.bean.Parking;
import com.estate.util.JsonResult;
import com.estate.util.ResultTool;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import static com.baomidou.mybatisplus.extension.toolkit.Db.count;

@RestController
@RequestMapping("/admin")
@Slf4j
public class  AdminController {

    @Resource
    private AdminService adminService;

    @GetMapping("list/{page}/{size}")
    public JsonResult list(@PathVariable("page") int page, @PathVariable("size") int size) {
        log.info("page:{},size:{}", page, size);
        JsonResult result = adminService.listByPage(page, size);
        log.info("result:{}", result);
        return result;
    }
    @PostMapping("/save")
    public JsonResult save(Manager manager) {
        log.info("新增manager:{}", manager);
        JsonResult result = adminService.saveManager(manager);
        log.info("result:{}", result);
        return result;
    }
    @PutMapping("/update")
    public JsonResult update(Manager manager) {
        log.info("修改manager:{}", manager);
        JsonResult result = adminService.updateManager(manager);
        log.info("result:{}", result);
        return result;
    }
    @DeleteMapping("/delete")
    public JsonResult delete(@RequestParam("mangerId") String mangerId) {
        log.info("删除manager:{}", mangerId);
        JsonResult result = adminService.deleteManager(mangerId);
        log.info("result:{}", result);
        return result;
    }
    @GetMapping("/list/{managerId}")
    public JsonResult list(@PathVariable("managerId") String managerId) {
        log.info("查询manager:{}", managerId);
        JsonResult result = ResultTool.success(adminService.getById(managerId));
        log.info("result:{}", result);
        return result;
    }

    @GetMapping("/findByCommunityId/{communityId}")
    public Long findByCommunityId(@PathVariable("communityId") String communityId) {
        log.info("communityId:{}", communityId);
        QueryWrapper<Manager> wrapper=new QueryWrapper();
        wrapper.eq("community_id",communityId);
        long count = adminService.count(wrapper);
        log.info("result:{}", count);
        return count;
    }
}